//
//  ZXJLoginViewController.m
//  academic_records
//
//  Created by zhang bozhao on 2020/8/5.
//  Copyright © 2020 zhang bozhao. All rights reserved.
//

#import "ZXJLoginViewController.h"
#import "ZXJPasswordLoginViewController.h"
#import "ZXJVerityCodeLoginViewController.h"
#import "ZXJHomePageViewController.h"
#import "WXApi.h"
#import <UMShare/UMShare.h>
#import "OffLineAlertViewController.h"
#import "AcademicTipsAlertViewController.h"
#import "AcademicTabbarViewController.h"
#import "ZXJHomePageViewController.h"
#import "LoginRequest.h"
#import "ZXJVerityCodeLoginViewController.h"
#import "WKWebViewController.h"

@interface ZXJLoginViewController ()
{
    NSString *_code;//用户换取access_token的code，仅在ErrCode为0时有效
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property(nonatomic,strong)WKWebViewController *webView;
@property (weak, nonatomic) IBOutlet UIButton *visitorLoginBTN;

@end

@implementation ZXJLoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.topHeight.constant = 0;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    [self setNeedsStatusBarAppearanceUpdate];
    self.webView = [[WKWebViewController alloc]init];
    NSNumber *visitor = [[NSUserDefaults standardUserDefaults]objectForKey:@"isIosVisitor"];
    _visitorLoginBTN.hidden =!visitor.boolValue;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userAgreenmentAction) name:@"userAgreenmentAction" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(securityAgreenmentAction) name:@"securityAgreenmentAction" object:nil];
}

- (IBAction)justScanAction:(id)sender {
    //游客登录
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"13758911" forKey:@"Phone"];
    [params setValue:@"123456" forKey:@"Passwd"];
    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"IPhoneUUID"] forKey:@"sessionID"];
    [LoginRequest do_lgoinByPassword:params :^(id  _Nullable data, ResultCode resultCode, NSError * _Nullable Error) {
        NSDictionary *dict = data;
        if (resultCode == SucceedCode) {
            AcademicTabbarViewController *minenav = [[AcademicTabbarViewController alloc]init];
            NSString *userid = [NSString stringWithFormat:@"%@",[dict objectForKey:@"uGuid"]];
            [[NSUserDefaults standardUserDefaults]setObject:userid forKey:@"USERID"];
            [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"PHONENUM"];
            [[NSUserDefaults standardUserDefaults]setObject:@(YES) forKey:@"IS_LOGIN"];
            [[NSUserDefaults standardUserDefaults]setObject:[NSMutableArray array] forKey:HistoryArr];
            APP_DELEGATE.window.rootViewController=minenav;
        }
    }];
}
- (IBAction)loginAction:(id)sender {
    MJWeakSelf
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        AcademicTipsAlertViewController *tipsAlertVC = [[AcademicTipsAlertViewController alloc]init];
        tipsAlertVC.alertType = 1;
        tipsAlertVC.block = ^(NSInteger index) {
            if (index == 0) {
                //手机快捷登录
              ZXJVerityCodeLoginViewController *verityVC = [[ZXJVerityCodeLoginViewController alloc]init];
              verityVC.type = 2;
              [weakSelf.navigationController pushViewController:verityVC animated:YES];
            }else{
              AcademicTipsAlertViewController *AlertVC = [[AcademicTipsAlertViewController alloc]init];
              AlertVC.alertType = 0;
              [AlertVC showInVC:weakSelf];
            }
        };
        [tipsAlertVC showInVC:self];
    }else{
        //手机快捷登录
      ZXJVerityCodeLoginViewController *verityVC = [[ZXJVerityCodeLoginViewController alloc]init];
      verityVC.type = 2;
      [weakSelf.navigationController pushViewController:verityVC animated:YES];
    }

}
-(void)userAgreenmentAction
{
    self.webView.titleStr = @"用户协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_ServiceUsage.html";
   [self.navigationController pushViewController:self.webView animated:YES];
}
-(void)securityAgreenmentAction
{
    self.webView.titleStr = @"隐私协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_Privacy.html";
   [self.navigationController pushViewController:self.webView animated:YES];
}
- (IBAction)wechatLogin:(id)sender {
    [self getAuthWithUserInfoFromWechat];
}
- (IBAction)passwordCodeLogin:(id)sender {
    ZXJPasswordLoginViewController *passwordVC = [[ZXJPasswordLoginViewController alloc]init];
    [self.navigationController pushViewController:passwordVC animated:YES];
}
- (IBAction)userAgreementAction:(id)sender {
    self.webView.titleStr = @"用户协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_ServiceUsage.html";
   [self.navigationController pushViewController:self.webView animated:YES];
}
- (IBAction)securityAction:(id)sender {
    self.webView.titleStr = @"隐私协议";
    self.webView.webUrl = @"http://app.zhishen.net.cn/xsz_h5/AppHtml/App_Privacy.html";
   [self.navigationController pushViewController:self.webView animated:YES];
    
}

- (void)getAuthWithUserInfoFromWechat
{
    MJWeakSelf
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"wechatLoginError=====%@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat unionid: %@", resp.unionId);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
            NSDictionary *dict =resp.originalResponse;
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setValue:dict[@"unionid"] forKey:@"unionID"];
            [params setValue:dict[@"nickname"]  forKey:@"nickName"];
            [params setValue:dict[@"headimgurl"]  forKey:@"headImg"];
            [params setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"IPhoneUUID"] forKey:@"sessionID"];
            [LoginRequest User_weixin_login:params :^(id  _Nullable data, ResultCode resultCode, NSError * _Nullable Error) {
                if (resultCode == SucceedCode) {
                    NSString *phone = [NSString stringWithFormat:@"%@",[data objectForKey:@"phone"]];
                    NSString *unionid = [NSString stringWithFormat:@"%@",[data objectForKey:@"unionID"]];
                    [[NSUserDefaults standardUserDefaults]setObject:unionid forKey:@"UNIONID"];
                    NSString *userid = [NSString stringWithFormat:@"%@",[data objectForKey:@"uGuid"]];
                    [[NSUserDefaults standardUserDefaults]setObject:userid forKey:@"USERID"];
                    [[NSUserDefaults standardUserDefaults]setObject:@(YES) forKey:@"IS_LOGIN"];
                    if (phone.length == 0) {
                        ZXJVerityCodeLoginViewController *changePhoneVC = [[ZXJVerityCodeLoginViewController alloc]init];
                        changePhoneVC.type = 1;
                        [weakSelf.navigationController pushViewController:changePhoneVC animated:YES];
                    }else{
                        [[NSUserDefaults standardUserDefaults]setObject:@"已绑定" forKey:@"BindingWeChat"];
                        [[NSUserDefaults standardUserDefaults]setObject:[data objectForKey:@"phone"] forKey:@"PHONENUM"];
                        [[NSUserDefaults standardUserDefaults]setObject:[NSMutableArray array] forKey:HistoryArr];
                        AcademicTabbarViewController *minenav = [[AcademicTabbarViewController alloc]init];
                        APP_DELEGATE.window.rootViewController=minenav;
                    }
                }
            }];
        }
    }];
}
// 字体设置为白色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
